import { yrLoginRequest} from "./index"

export function getLoginCode(){
  return new Promise((resolve,reject)=>{
    wx.login({
      timeout: 1000,  //超时时间
      success: (result) => { //调用成功回调
        const code=result.code
        resolve(code)
      },
      fail: err => {    //调用失败回调
        reject(err)
      },
    })
  })
}

export function codeToToken(code){
  return yrLoginRequest.post("/login",{code})
}

export function checkToken(){
  return yrLoginRequest.post("/auth",{},true)
}

export function checkSession(){
  return new Promise((resolve)=>{
    //检查登录态是否过期
    wx.checkSession({ 
      success:()=>{
        resolve(true)
      },
      fail:()=>{
        resolve(false)
      }
    })
  })
}

export function getUserInfo(){
  return new Promise((resolve,reject)=>{
    wx.getUserProfile({
      desc: '信息用途',
      success: (res) => {
        resolve(res)
      },
      fail:(err)=>{
        reject(err)
      }
    })
  })
}